package com.softline.documentsmall.service.impl;


import com.softline.documentsmall.dao.HotIssuesMapper;
import com.softline.documentsmall.entity.HotIssues;
import com.softline.documentsmall.ret.BusinessErrorEnum;
import com.softline.documentsmall.ret.ReturnVO;
import com.softline.documentsmall.ret.ReturnVOUtils;
import com.softline.documentsmall.service.HotIssuesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author zhongweicheng
 * @date 2020/8/25 15:46
 */
@Service
@Slf4j
public class HotIssuesServiceImp implements HotIssuesService {

    @Value(value = "${pageParams.currPage}")
    private Integer currPageCfg;

    @Value(value = "${pageParams.pageSize}")
    private Integer pageSizeCfg;



    @Autowired
    HotIssuesMapper hotIssuesMapper;

    @Override
    public ReturnVO findByParams(Integer id, Integer isDelete, String content, String title, Integer currPage, Integer pageSize, String startDate, String endDate) {

        //分页参数,不传则使用默认值
        if(Objects.isNull(currPage)) {
            currPage = currPageCfg;
        }
        if(Objects.isNull(pageSize)) {
            pageSize = pageSizeCfg;
        }
        return ReturnVOUtils.success(hotIssuesMapper.findByParams(id, isDelete,content, title,(currPage-1)*pageSize,pageSize,startDate,endDate));
    }


    @Override
    public ReturnVO insertHotIssue(HotIssues hotIssues) {
        try{
            hotIssuesMapper.insertHotIssue(hotIssues);
            return ReturnVOUtils.success();
        }catch (Exception e){
            log.error(" insertHotIssue exception{}",e.getMessage());
        }
        return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
    }

    /**
     * 根据id更新，全量传递数据
     * @param hotIssues
     * @return
     */
    @Override
    public ReturnVO updateHotIssueById(HotIssues hotIssues) {

        try{
            hotIssuesMapper.updateHotIssueById(hotIssues);
            return ReturnVOUtils.success();
        }catch (Exception e){
            log.error(" updateHotIssueById exception{}",e.getMessage());
        }
        return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL) ;
    }

    /**
     * 根据id软删除问题
     * @param id
     */
    @Override
    public ReturnVO softDeleteHotIssueById(Integer id) {
        try{
            hotIssuesMapper.softDeleteHotIssueById(id);
            return ReturnVOUtils.success();
        }catch (Exception e){
            log.error(" softDeleteHotIssueById exception{}",e.getMessage());

        }
        return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
    }
}
